package kr.zetaomicron.ultrasolitaire;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import kr.zetaomicron.framework.FileIO;
import kr.zetaomicron.framework.math.Vector2;

class Scores {
	float timeElapsed;	int deaths, rotation;
}

public class Settings {
	public static short portrait = 1;
	public final static short PORT = -90;
	// 													rotEnable, tutorial, rotTimes
	public static boolean[] enabledFunc = new boolean[] {false, false, false};
	//												 from, to, count, anim.time, autotime(0 == no, 1 == yes)
	public static float[] anchor2anchor = new float[] {0, 0, 0, 1.0f, 0};
    public static boolean soundEnabled = true, tmpGameSettings = false, gameRunning = false;
    public final static int[] highscores = new int[] { 100, 80, 50, 30, 10 };
    public final static String file = ".ultrasol";
    public static int nLevelRows = 0, nLevelCols = 0;
    public final static int WORLD_WIDTH = 720, WORLD_HEIGHT = 1280;
    public static List<String> line_conv;
    public final static int BTN_SCROLL_WIDTH = 40; 
    public static int[] gameSettings = new int[] {0, 0, 0, 0};	// suits, decks, ranks, deal
    public static int[] gameSettings_max = new int[] {2, 3, 2, 1};	// suits, decks, ranks, deal
    public static int columnNum = 16, MIN_COLUMN_NUM = 10, MAX_COLUMN_NUM = 20;	// number of columns, min max number of columns
    public static float[] columnIndices = new float[] {0, 0, 0};	// index of left tableau, foundation
    public static float[] anchorNum = new float[] {7, 7};	// number of tableaus and foundations
    public static boolean autoplayFlag = true;
    public static Scores[] scores = new Scores[12];
    public static Scores curScores = new Scores();

    public static void initCurScores() {
    	curScores.timeElapsed = 0.0f;	curScores.deaths = 0;	curScores.rotation = -1;
    }

    public static void load(FileIO files) {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(files.readFile(file)));
            soundEnabled = Boolean.parseBoolean(in.readLine());
            for(int i = 0; i < 5; i++) {
                highscores[i] = Integer.parseInt(in.readLine());
            }
        } catch (IOException e) {
            // :( It's ok we have defaults
        } catch (NumberFormatException e) {
            // :/ It's ok, defaults save our day
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
            }
        }
        
//        try {
//	        BufferedReader reader = new BufferedReader(new InputStreamReader(files.readAsset("1.raw")));
//	        String line = reader.readLine();
//	        String line_conv = "";
//	        for (int i = 0; i < line.length(); i++) {
//	        	if (0 == line.charAt(i))
//	        		line_conv += "0";
//	        	else
//	        		line_conv += "1";
//	        }
//	        Log.i("Settings", "[load] line: " + line_conv);
//	    } catch (IOException e) {
//	        // :( It's ok we have defaults
//	    } catch (NumberFormatException e) {
//	        // :/ It's ok, defaults save our day
//	    } finally {
//	        try {
//	            if (in != null)
//	                in.close();
//	        } catch (IOException e) {
//	        }
//	    }
//        
//        Log.i("Settings", "[load] finished.");
    }
    
    public static Vector2 getBoundsFromSprite(float x, float y, float w, float h) {
    	Vector2 vector = new Vector2(x - w / 2, y - h / 2);
    	return vector;
    }

    public static void save(FileIO files) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    files.writeFile(file)));
            out.write(Boolean.toString(soundEnabled));
            out.write("\n");
            for(int i = 0; i < 5; i++) {
                out.write(Integer.toString(highscores[i]));
                out.write("\n");
            }

        } catch (IOException e) {
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }
    }

    public static void addScore(int score) {
        for(int i=0; i < 5; i++) {
            if(highscores[i] < score) {
                for(int j= 4; j > i; j--)
                    highscores[j] = highscores[j-1];
                highscores[i] = score;
                break;
            }
        }
    }
    
    public static void setCardAnim(int idxFrom, int idxTo, int count, boolean autoplay) {
    	anchor2anchor[0] = idxFrom;
		anchor2anchor[1] = idxTo;
		anchor2anchor[2] = count;
		anchor2anchor[3] = 0.0f;
		anchor2anchor[4] = autoplayFlag && autoplay ? 1 : 0;
    }
}
